home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Just Call Me Internet
/
Just Call Me Internet.iso
/
prog
/
mint
/
duftp
/
ls2filel.c
< prev
next >
Wrap
Text File
|
1995-06-15
|
3KB
|
142 lines
/*
DUFTP
*/
// 'ls -l' listing to file_list conversion
#include <DULIB.H>
#include <STDLIB.H>
#include <STDIO.H>
#include "ls2filel.h"
#include "globals.h"
file_list *ls_to_list(char *filename)
{
char buffer[500];
char *buf_ptr;
char buf2[FMSIZE];
FILE *f;
file_list *flist;
file_list *rtn_list;
file_list *flist_c;
f=fopen(filename,"r");
if (f)
{
fgets(buffer,499,f); // Skip the header line 'total n'
rtn_list=flist=NULL;
while (feof(f)==0)
{
flist_c=(file_list*)malloc(sizeof(file_list));
fgets(buffer,499,f); // Read file line
buf_ptr=buffer;
if (!feof(f))
{
if ((buf_ptr[0]=='d') // Is this a directory?
||((((buf_ptr[0]=='l')&&(buf_ptr[3]=='x'))&&(buf_ptr[6]=='x'))&&(buf_ptr[9]=='x')))
flist_c->is_directory=TRUE;
else
flist_c->is_directory=FALSE;
if (buf_ptr[0]=='l') // Is this a directory?
flist_c->is_symlink=TRUE;
else
flist_c->is_symlink=FALSE;
if (buf_ptr[1]=='r') // Can the owner read it?
flist_c->owner_read=TRUE;
else
flist_c->owner_read=FALSE;
if (buf_ptr[7]=='r') // Can the general pulic read it?
flist_c->public_read=TRUE;
else
flist_c->public_read=FALSE;
for(buf_ptr=buf_ptr+10; *buf_ptr==' '; buf_ptr++);
for(; *buf_ptr!=' '; buf_ptr++);
for(buf_ptr=buf_ptr+1; *buf_ptr==' '; buf_ptr++);
sscanf(buf_ptr,"%s", buf2); // Get file owner
buf2[19]='\0';
sprintf(flist_c->owner,"%s",buf2);
for(buf_ptr=buf_ptr+strlen(buf2)+1; *buf_ptr==' '; buf_ptr++);
for(; *buf_ptr!=' '; buf_ptr++);
for(buf_ptr=buf_ptr+1; *buf_ptr==' '; buf_ptr++);
sscanf(buf_ptr,"%s",buf2); // Get file size
flist_c->size=atol(buf2); // convert file size into a long
for(buf_ptr=buf_ptr+strlen(buf2)+1; *buf_ptr==' '; buf_ptr++);
for(; *buf_ptr!=' '; buf_ptr++);
for(buf_ptr=buf_ptr+1; *buf_ptr==' '; buf_ptr++);
for(; *buf_ptr!=' '; buf_ptr++);
for(buf_ptr=buf_ptr+1; *buf_ptr==' '; buf_ptr++);
for(; *buf_ptr!=' '; buf_ptr++);
sscanf(buf_ptr,"%s",buf2); // get the file name
if (flist_c->is_directory)
sprintf(flist_c->name," %s",buf2); // Mark directories
else
sprintf(flist_c->name," %s (%d)",buf2,flist_c->size);
if (flist_c->is_symlink) // mark symlinks so we know they're there
flist_c->name[1]='';
flist_c->next=flist;
flist=flist_c;
}else{
free(flist_c);
}
}
fclose(f);
}
return flist;
}
char **extract_filenames(file_list *l)
{
file_list *f;
char **t;
short n,cnt;
if (!l) return NULL;
file_count=0;
for(f=l; f!=NULL; f=f->next) file_count++; // Count up how many files we have
cnt=file_count; if (cnt<6) cnt=6;
t=(char**)malloc(sizeof(char*)*(cnt+2));
n=file_count;
for(f=l; n>0; f=f->next) // Extract the names
{
t[n]=f->name;
n--;
}
t[0]=" .."; // Add a parent directory entry
if (cnt>file_count) // Pad so we don't get empty file lists
{
for(n=file_count+1; n<cnt+1; n++)
t[n]=" ";
}
return t;
}
void dispose_file_list(file_list *f)
{
file_list *n;
while(f)
{
n=f;
f=f->next;
free(n);
}
}